package io.renren.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;

import io.renren.entity.SysProductEntity;
import io.renren.service.SysProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-10 08:55:53
 */
@RestController
@RequestMapping("api/sysproduct")
@Api(tags = "商品")
public class SysProductController {
    @Autowired
    private SysProductService sysProductService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysProductService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("根据ID查询产品")
    public R info(@PathVariable("id") Integer id){
        SysProductEntity sysProduct = sysProductService.getById(id);

        return R.ok().put("sysProduct", sysProduct);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加商品")
    public R save(@RequestBody SysProductEntity sysProduct){
        sysProductService.save(sysProduct);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改商品")
    public R update(@RequestBody SysProductEntity sysProduct){
        ValidatorUtils.validateEntity(sysProduct);
        sysProductService.updateById(sysProduct);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(Integer[] ids){
        sysProductService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 查询门店可进货商品
     */
    @GetMapping("retailOptionalProducts")
    @ApiOperation("查询门店可进货商品")
    public R retailOptionalProducts(@RequestParam(required = false) Map<String, Object> params) {
        PageUtils pageUtils = sysProductService.retailOptionalProducts(params);
        return R.ok().put("page", pageUtils);
    }

}
